﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace JV.MVVM {
    public partial class BindingEditorForm: Form {

        TextHighlighter th;
        Scanner scanner;
        Parser parser;

        public string Bindings { get; set; }

        public BindingEditorForm( ) {
            InitializeComponent( );
        }

        private void BindingEditorForm_Load( object sender, EventArgs e ) {

            scanner = new Scanner( );
            parser = new Parser( scanner );
            richTextBox1.Rtf = Properties.Resources.MVVMHelp;

        }

        private void BindingEditorForm_Shown( object sender, EventArgs e ) {
            rtbBindings.Text = Bindings;
        }

        private void btnAccept_Click( object sender, EventArgs e ) {
            Bindings = rtbBindings.Text;
        }

        private void rtbBindings_TextChanged( object sender, EventArgs e ) {

            tmrHighLight.Stop( );
            tmrHighLight.Start( );

        }

        private void tmrHighLight_Tick( object sender, EventArgs e ) {
            tmrHighLight.Stop( );

            var parseTree = parser.Parse( rtbBindings.Text );

            if ( parseTree.Errors.Count > 0 ) return;

            th = new TextHighlighter( rtbBindings, scanner, parser );
            th.HighlightText( );
            th.Dispose( );
        }

    }
}
